package hongniang

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"time"
)

type HongniangjifenBody struct {
	Hongniangid int64 `json:"hongniangid" gorm:"hongniangid"`
}

func Hongniangjifen(ctx *gin.Context) {
	request := HongniangjifenBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	var hongniang models.Hongniang
	data_source.Db.Select("points").Where("id = ?", hongniangid).First(&hongniang)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    hongniang.Points,
		"msg":     "ok",
	})
	return

}

func Qiandao(ctx *gin.Context) {
	request := HongniangjifenBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	now := time.Now()
	zeroToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Unix()
	var qiandao models.Hongniangqiandao
	data_source.Db.Where("hongniangid = ?", hongniangid).Where("daytime = ?", zeroToday).First(&qiandao)
	if qiandao.ID > 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请勿重复签到",
		})
		return
	}

	qdadd := models.Hongniangqiandao{
		Hongniangid: hongniangid,
		Daytime:     zeroToday,
	}
	data_source.Db.Create(&qdadd)
	var congfig models.Config
	data_source.Db.Where("id = 5").First(&congfig)

	points := models.Hongniangpoints{
		Hongniangid: hongniangid,
		Points:      congfig.Value,
		Addtime:     time.Now().Unix(),
		Title:       "签到获得积分",
		State:       1,
	}
	data_source.Db.Create(&points)
	data_source.Db.Model(&models.Hongniang{}).Where("id = ?", hongniangid).Update("points", gorm.Expr("points + ?", congfig.Value))
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Qiandaolist(ctx *gin.Context) {
	request := HongniangjifenBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	monthstart, _ := getCurrentMonthStartAndEnd()
	var qiandaos []models.Hongniangqiandao
	data_source.Db.Where("hongniangid = ?", hongniangid).Where("daytime >= ?", monthstart).Order("id desc").Find(&qiandaos)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    qiandaos,
		"msg":     "ok",
	})
	return
}
func Pointslist(ctx *gin.Context) {
	request := HongniangjifenBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	monthstart, _ := getCurrentMonthStartAndEnd()
	var qiandaos []models.Hongniangpoints
	data_source.Db.Where("hongniangid = ?", hongniangid).Where("addtime >= ?", monthstart).Order("id desc").Find(&qiandaos)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    qiandaos,
		"msg":     "ok",
	})
	return
}
func Jifenconfig(ctx *gin.Context) {
	var config []models.Config
	data_source.Db.Where("id = 5 or id = 6 or id = 7 or id = 8").Find(&config)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    config,
		"msg":     "ok",
	})
	return

}

func Hongniangjifenqingling(ctx *gin.Context) {
	request := HongniangjifenBody{}
	ctx.ShouldBind(&request)

	data_source.Db.Where("points != ?", 0).Update("points", 0)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

// 本月
func getCurrentMonthStartAndEnd() (startTime, endTime int64) {
	// 获取当前时间
	now := time.Now()

	// 获取本月的第一天
	start := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)

	// 获取下个月的第一天减去一秒，得到本月的最后一秒
	end := start.AddDate(0, 1, 0).Add(-1 * time.Second)

	return start.Unix(), end.Unix()
}
